package sz.szsmk.citizencard.ui.adapter;

import android.os.Bundle;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.AdressListViewBinding;
import sz.szsmk.citizencard.model.network.bean.response.AdressData;
import sz.szsmk.citizencard.model.network.bean.response.QueryAdressResponse;
import sz.szsmk.citizencard.ui.activity.AdressAdminAvtivity;
import sz.szsmk.citizencard.util.JumpUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/10 16:28
 * description:地址管理
 */
public class AdressAdminAdapter extends RecyclerView.Adapter<AdressAdminAdapter.AdressAdminHolder> {

    private AdressAdminAvtivity mContext;
    private List<QueryAdressResponse.DataBean> mData;

    private DelArrressInterface delArrressInterface;
    private SetAddressInterface setAddressInterface;
    private ItemAdressInterface itemAddressInterface;
    private int order = 0;
    private String mOrderData;

//    private int selected = -1;

    private int mSelectedPos = -1;//实现单选  方法二，变量保存当前选中的position


    private ArrayList<String> title = new ArrayList<>();

    public AdressAdminAdapter(AdressAdminAvtivity context) {
        mContext = context;
        title.add("天气");
        title.add("旅游");
        title.add("小秘书（平台）");
        title.add("优惠");
    }

    public void delSetOnClick(DelArrressInterface mdelArrressInterface) {
        delArrressInterface = mdelArrressInterface;
    }

    public void setAdressOnClick(SetAddressInterface setAddressInterface) {
        this.setAddressInterface = setAddressInterface;
    }

    public void itemAdressOnClick(ItemAdressInterface itemAddressInterface) {
        this.itemAddressInterface = itemAddressInterface;
    }


    public void updateData(List<QueryAdressResponse.DataBean> data) {
        if (data != null) {
            this.mData = data;
        }
        notifyDataSetChanged();
    }

    public interface SetAddressInterface {
        public void onClick(View view, int position);
    }

    public interface ItemAdressInterface {
        public void onClick(View view, int position);
    }


    /**
     * 定义删除、编辑及设置默认地址回调接口
     */
    public interface DelArrressInterface {
        public void onClick(View view, int position);
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.adress_list_view;
    }


    @Override
    public int getItemCount() {
        return null == mData ? 0 : mData.size();
    }

    @Override
    public AdressAdminHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return AdressAdminHolder.creat(parent);
    }

    @Override
    public void onBindViewHolder(final AdressAdminHolder holder, int position) {

        loadItemData(position, holder.binding);

    }

    private void loadItemData(final int position, final AdressListViewBinding binding) {
        binding.adressDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (delArrressInterface != null) {
                    delArrressInterface.onClick(v, position);
                }
            }
        });
        if (mData != null) {
            for (int i = 0; i < mData.size(); i++) {
                if (mData.get(i).getIsDefault() == 1) {
                    mSelectedPos = i;
                }
            }
        }

        binding.adressSetdefault.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    binding.adressSetdefault.setEnabled(false);
                } else {
                    binding.adressSetdefault.setEnabled(true);
                }
            }
        });

        binding.adressEdit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                QueryAdressResponse.DataBean dataBean = mData.get(position);
                String address = dataBean.getAddress();
                String name = dataBean.getContact();
                String mobile = dataBean.getMobile();
                String location = dataBean.getLocation();
                int addressId = dataBean.getAddressId();
                int isDefault = dataBean.getIsDefault();
                String contact = dataBean.getContact();
                Bundle bundle = new Bundle();
                bundle.putString("key", "edit");
                bundle.putInt("addressId", addressId);
                bundle.putString("address", address);
                bundle.putString("name", name);
                bundle.putString("isDefault", isDefault + "");
                bundle.putString("mobile", mobile);
                bundle.putString("location", location);
                bundle.putString("contact", contact);
                JumpUtils.jumptoNatvie(mContext, "adressadd", 0, bundle);

            }
        });
        binding.adressItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (order == 1) {
                    AdressData adressData = new Gson().fromJson(mOrderData, AdressData.class);

                    Bundle bundle = new Bundle();
                    int addressId = mData.get(position).getAddressId();
                    String url = adressData.getRedirectUrl() + "?tradeType=" + adressData.getTradeType() + "&orderNo=" + adressData.getOrderNo() + "&addressId=" + addressId;
                    bundle.putString("url", url);
                    JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
                    mContext.finish();
                } else {
                    return;
                }

            }
        });
        if (mData == null) {
            Toast.makeText(mContext, "您尚未添加任何收货地址", Toast.LENGTH_SHORT).show();
        }
        binding.adressUserName.setText(mData.get(position).getContact());
        binding.adressDesAdress.setText(mData.get(position).getLocation() + mData.get(position).getAddress());
        binding.adressUserPhone.setText(mData.get(position).getMobile());
        int isDefault = mData.get(position).getIsDefault();
        if (isDefault == 0) {
            binding.adressSetdefault.setChecked(false);
        } else {
            binding.adressSetdefault.setChecked(true);
        }
        Boolean isSele = false;
        if (mData.get(position).getIsDefault() == 1) {
            isSele = true;
        } else {
            isSele = false;
        }
        binding.adressSetdefault.setSelected(isSele);
        binding.adressSetdefault.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSelectedPos != position) {
                    //先取消上个item的勾选状态
                    if (mSelectedPos != -1) {
                        mData.get(mSelectedPos).setIsDefault(0);
                        notifyItemChanged(mSelectedPos);
                        mSelectedPos = position;
                        mData.get(mSelectedPos).setIsDefault(1);
//                    mDatas.get(mSelectedPos).setSelected(true);
                        notifyItemChanged(mSelectedPos);
                    }
                }
                if (setAddressInterface != null) {
                    setAddressInterface.onClick(v, position);
                }
            }
        });
    }

    public void setAdressData(List<QueryAdressResponse.DataBean> data, int order, String arg) {
        mData = data;
        this.order = order;
        mOrderData = arg;
    }


    public static class AdressAdminHolder extends RecyclerView.ViewHolder {
        private AdressListViewBinding binding;

        public static AdressAdminHolder creat(ViewGroup parent) {
            AdressListViewBinding binding = AdressListViewBinding
                    .inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new AdressAdminHolder(binding);
        }

        public AdressAdminHolder(AdressListViewBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
